# !/usr/bin/python
# -*- coding: UTF-8 -*-

import configparser
import random
import calendar_test


"""
根据不同情况分析
只有是0的情况,会存在无绩效的情况.在其他的情况下,都是存在绩效工资的
绩效工资不能太低,通常情况下,在200+

1.工资比较高,满勤+周末满勤+绩效
2.满勤+周末不满勤快+绩效
3.平时出勤大于10,但是不满勤+周末加班+绩效
4.平时出勤小于10+绩效
"""
ini_file = configparser.ConfigParser()
ini_file.read('./config.ini', 'utf-8')


year = int(ini_file.get('ann', 'year'))
month = int(ini_file.get('ann', 'month'))

# 以下参数可以通过直接获取的方式，避免手工填写

# 设置本月工作天数
month_work_day = int(ini_file.get('ann', 'month_work_day'))

msg = ini_file.get('ann', 'month_weekend_list')
month_weekend_list = msg.split(',')
# 设置本月周末天数
month_weekend = len(month_weekend_list)

###################################################################################
# 时薪
salary_hour = int(ini_file.get('ann', 'salary_hour'))

# 加班时薪
salary_hour_workday_overtime = salary_hour * 1.5
# 周六加班时薪
salary_hour_weekend_overtime = salary_hour * 2
###################################################################################
# 全考勤
salary_workday = salary_hour * month_work_day * 8
# 全考勤加班工资
salary_workday_overtime = salary_hour * 1.5 * month_work_day * 2
# 全考勤周六工资
salary_weekend_overtime = salary_hour * 2 * month_weekend * 8
###################################################################################
# 日薪
# 基本的思路,保证上班时间尽量满足加班.即如果正常上班,也正常加班
# 正常工作日 日薪
salary_one_day = salary_hour * 8 + salary_hour_workday_overtime * 2
# 周六日薪
salary_weekend_day = salary_hour_weekend_overtime * 8


def calc_extra(pay, reality_work_day, reality_weekend_day):
    """
    非绩效工资 = 实际工作天数 * 通常情况一天的薪水 + 加班薪水 * 加班天数
    :param pay: 应发合计
    :param reality_work_day:  实际工作天数
    :param reality_weekend_day: 周末加班天数
    :return: 绩效工资 = 应发合计 - 非绩效工资
    """
    remove_extra = reality_work_day * salary_one_day + reality_weekend_day * salary_weekend_day
    return pay - remove_extra


def get_reality_normal_work_day(pay):
    """
    针对实发金额较少的情况
    保证绩效在120+,实际工作的天数
    :param pay: 应发合计
    :return:
    """
    reality_normal_work_day = int(pay / salary_one_day)
    salary = reality_normal_work_day * salary_one_day
    while pay - salary < 120:
        reality_normal_work_day -= 1
        # 重新计算
        salary = reality_normal_work_day * salary_one_day

    return reality_normal_work_day


def calc_day(pay):
    if pay == 0:
        # 羡慕不用来上班的
        return {
            'salary_hour': salary_hour,
            'bank_card_no': '请假',
            'work_day': 0,
            'work_weekend': 0,
            'rest_weekend': month_weekend,
            'rest_weekend_detail': 'TODO',
            'takeOff': month_work_day,
            'takeOff_detail': 'TODO一整个月',
            'is_leave_application': None,
            'normal_work_time': 0,
            'normal_work_salary': 0,
            'workday_overtime_time': 0,
            'workday_overtime_salary': 0,
            'weekend_overtime_time': 0,
            'weekend_overtime_salary': 0,
            'extra': 0
        }
    elif pay > salary_one_day * month_work_day + salary_weekend_day * month_weekend + 220:
        # 满勤+周末满勤+绩效
        extra = calc_extra(pay, month_work_day, month_weekend)
        return {
            'salary_hour': salary_hour,
            'bank_card_no': None,
            'work_day': month_work_day,
            'work_weekend': month_weekend,
            'rest_weekend': 0,
            'rest_weekend_detail': None,
            'takeOff': 0,
            'takeOff_detail': 0,
            'is_leave_application': None,
            'normal_work_time': month_work_day * 8,
            'normal_work_salary': month_work_day * 8 * salary_hour,
            'workday_overtime_time': month_work_day * 2,
            'workday_overtime_salary': month_work_day * 2 * salary_hour * 1.5,
            'weekend_overtime_time': month_weekend * 8,
            'weekend_overtime_salary': month_weekend * salary_weekend_day,
            'extra': round(extra, 2)
        }
    elif pay > salary_one_day * month_work_day + 1 * salary_weekend_day + 220:
        # 满勤 周末不满勤
        reality_normal_weekend_day = month_weekend
        while calc_extra(pay, month_work_day, reality_normal_weekend_day) < 220:
            reality_normal_weekend_day -= 1
        extra = calc_extra(pay, month_work_day, reality_normal_weekend_day)

        # 获取周末休息时间
        rest_weekend_detail_list = calendar_test.get_weekend(year, month)
        rest_weekend_detail = '/'.join(rest_weekend_detail_list[:month_weekend - reality_normal_weekend_day])

        return {
            'salary_hour': salary_hour,
            'bank_card_no': None,
            'work_day': month_work_day,
            'work_weekend': reality_normal_weekend_day,
            'rest_weekend': month_weekend - reality_normal_weekend_day,
            'rest_weekend_detail': rest_weekend_detail,
            'takeOff': 0,
            'takeOff_detail': 0,
            'is_leave_application': None,
            'normal_work_time': month_work_day * 8,
            'normal_work_salary': month_work_day * 8 * salary_hour,
            'workday_overtime_time': month_work_day * 2,
            'workday_overtime_salary': month_work_day * 2 * salary_hour * 1.5,
            'weekend_overtime_time': reality_normal_weekend_day * 8,
            'weekend_overtime_salary': reality_normal_weekend_day * salary_weekend_day,
            'extra': round(extra, 2)
        }
    elif pay < 2000:
        # 周末别来了,求你了 = =!
        reality_normal_work_day = get_reality_normal_work_day(pay)
        extra = pay - reality_normal_work_day * salary_one_day

        # 获取周末休息时间
        rest_weekend_detail_list = calendar_test.get_weekend(year, month)
        rest_weekend_detail = '/'.join(rest_weekend_detail_list[:month_weekend])
        print(pay)
        print(extra)
        print(month_work_day - reality_normal_work_day)
        # 获取请假详情
        takeOff_detail = calendar_test.get_no_work_day(year, month, month_work_day - reality_normal_work_day)

        return {
            'salary_hour': salary_hour,
            'bank_card_no': '请假',
            'work_day': reality_normal_work_day,
            'work_weekend': 0,
            'rest_weekend': month_weekend,
            'rest_weekend_detail': rest_weekend_detail,
            'takeOff': month_work_day - reality_normal_work_day,
            'takeOff_detail': takeOff_detail,
            'is_leave_application': None,
            'normal_work_time': reality_normal_work_day * 8,
            'normal_work_salary': reality_normal_work_day * 8 * salary_hour,
            'workday_overtime_time': reality_normal_work_day * 2,
            'workday_overtime_salary': reality_normal_work_day * 2 * salary_hour * 1.5,
            'weekend_overtime_time': 0,
            'weekend_overtime_salary': 0,
            'extra': round(extra, 2)
        }
    else:
        # 构造周末加班天数,随机一个数字
        reality_normal_weekend_day = random.randint(0, month_weekend)
        reality_normal_work_day = month_work_day
        # TODO 存在一种常识  周末加班天数 * 5 <= 休息天数(本月工作日 - 实际工作天数)
        extra = pay - reality_normal_weekend_day * salary_weekend_day - reality_normal_work_day * salary_one_day
        limit = random.randint(150, 220)
        while extra < limit:
            reality_normal_work_day -= 1
            extra = pay - reality_normal_weekend_day * salary_weekend_day - reality_normal_work_day * salary_one_day

        # 获取周末休息时间
        if reality_normal_weekend_day == month_weekend:
            rest_weekend_detail = None
        else:

            rest_weekend_detail_list = calendar_test.get_weekend(year, month)
            rest_weekend_detail = '/'.join(rest_weekend_detail_list[:month_weekend - reality_normal_weekend_day])

        # 获取请假详情
        takeOff_detail = calendar_test.get_no_work_day(year, month, month_work_day - reality_normal_work_day)

        return {
            'salary_hour': salary_hour,
            'bank_card_no': '请假',
            'work_day': reality_normal_work_day,
            'work_weekend': reality_normal_weekend_day,
            'rest_weekend': month_weekend - reality_normal_weekend_day,
            'rest_weekend_detail': rest_weekend_detail,
            'takeOff': month_work_day - reality_normal_work_day,
            'takeOff_detail': takeOff_detail,
            'is_leave_application': None,
            'normal_work_time': reality_normal_work_day * 8,
            'normal_work_salary': reality_normal_work_day * 8 * salary_hour,
            'workday_overtime_time': reality_normal_work_day * 2,
            'workday_overtime_salary': reality_normal_work_day * 2 * salary_hour * 1.5,
            'weekend_overtime_time': reality_normal_work_day * 8,
            'weekend_overtime_salary': reality_normal_weekend_day * salary_weekend_day,
            'extra': round(extra, 2)
        }


if __name__ == '__main__':
    # work_day(2665.25)
    # work_day(3484)

    d = calc_day(316.3)
    print(d)
    print('--------------')
